package com.ruoyi.web.controller.production.stroma;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.stroma.StromaType;
import com.ruoyi.production.service.stroma.IStromaTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基质类型Controller
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@RestController
@RequestMapping("/production/stromaType")
public class StromaTypeController extends BaseController
{
    @Autowired
    private IStromaTypeService stromaTypeService;

    /**
     * 查询基质类型列表
     */
    @PreAuthorize("@ss.hasPermi('production:stromaType:list')")
    @GetMapping("/list")
    public TableDataInfo list(StromaType stromaType)
    {
        startPage();
        List<StromaType> list = stromaTypeService.selectStromaTypeList(stromaType);
        return getDataTable(list);
    }

    /**
     * 导出基质类型列表
     */
    @PreAuthorize("@ss.hasPermi('production:stromaType:export')")
    @Log(title = "基质类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StromaType stromaType)
    {
        List<StromaType> list = stromaTypeService.selectStromaTypeList(stromaType);
        ExcelUtil<StromaType> util = new ExcelUtil<StromaType>(StromaType.class);
        return util.exportExcel(list, "基质类型数据");
    }

    /**
     * 获取基质类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:stromaType:query')")
    @GetMapping(value = "/{stromaTypeId}")
    public AjaxResult getInfo(@PathVariable("stromaTypeId") Long stromaTypeId)
    {
        return AjaxResult.success(stromaTypeService.selectStromaTypeById(stromaTypeId));
    }

    /**
     * 新增基质类型
     */
    @PreAuthorize("@ss.hasPermi('production:stromaType:add')")
    @Log(title = "基质类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StromaType stromaType)
    {
        try {
            return toAjax(stromaTypeService.insertStromaType(stromaType));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("名称已存在！");
            }else return error("失败!");
        }
    }

    /**
     * 修改基质类型
     */
    @PreAuthorize("@ss.hasPermi('production:stromaType:edit')")
    @Log(title = "基质类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StromaType stromaType)
    {
        try {
            return toAjax(stromaTypeService.updateStromaType(stromaType));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("名称已存在！");
            }else return error("失败!");
        }
    }

    /**
     * 删除基质类型
     */
    @PreAuthorize("@ss.hasPermi('production:stromaType:remove')")
    @Log(title = "基质类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stromaTypeIds}")
    public AjaxResult remove(@PathVariable Long[] stromaTypeIds)
    {
        return toAjax(stromaTypeService.deleteStromaTypeByIds(stromaTypeIds));
    }
}
